﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;

namespace camcad7Home.Controllers
{
    public class MonController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Mon/
       public ActionResult jsonList()
        {
            string a = Request.Cookies["_culture"]["lang"];

            var list = from m in db.Mons where a==m.LangID select new { m.ID,m.Name,MonCha=m.MonCha.Name};
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /Mon/Details/5
        public ActionResult XemMon(int ID)
        {
            Mon mon = db.Mons.Find(ID);
            if (mon == null)
            {
                return HttpNotFound();
            }

            return View(mon);
        }
        public ActionResult Details(int id = 0)
        {
            Mon mon = db.Mons.Find(id);
            if (mon == null)
            {
                return HttpNotFound();
            }
            return View(mon);
        }

        //
        // GET: /Mon/Create

        public ActionResult Create()
        {     string a = Request.Cookies["_culture"]["lang"];
           ViewBag.SubjectID = new SelectList(db.Subjects.Where(m=>m.LangID==a), "ID", "Name");
           ViewBag.ParentID = new SelectList(db.Mons.Where(m => m.LangID == a && m.ParentID == null), "ID", "Name");
            return View();
        }

        //
        // POST: /Mon/Create
         [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Mon mon)
        {
            mon.LangID = Request.Cookies["_culture"]["lang"];
            if (ModelState.IsValid)
            {
                mon.NoiDung = HttpUtility.HtmlEncode(mon.NoiDung);
                db.Mons.Add(mon);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.SubjectID = new SelectList(db.Subjects.Where(m => m.LangID == a), "ID", "Name");
            ViewBag.ParentID = new SelectList(db.Mons.Where(m => m.LangID == a && m.ParentID == null), "ID", "Name", mon.ParentID);
            return View(mon);
        }

        //
        // GET: /Mon/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Mon mon = db.Mons.Find(id);
            if (mon == null)
            {
                return HttpNotFound();
            }
            mon.NoiDung = HttpUtility.HtmlDecode(mon.NoiDung);
            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.SubjectID = new SelectList(db.Subjects.Where(m => m.LangID == a), "ID", "Name",mon.SubjectID);
            ViewBag.ParentID = new SelectList(db.Mons.Where(m => m.LangID == a && m.ParentID == null&&m.ID!=mon.ID), "ID", "Name");
            return View(mon);
        }

        //
        // POST: /Mon/Edit/5
           [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Mon mon)
        {
            if (ModelState.IsValid)
            {
                mon.NoiDung = HttpUtility.HtmlEncode(mon.NoiDung);
                db.Entry(mon).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.SubjectID = new SelectList(db.Subjects.Where(m => m.LangID == a), "ID", "Name", mon.SubjectID);
            ViewBag.ParentID = new SelectList(db.Mons.Where(m => m.LangID == a && m.ParentID == null && m.ID != mon.ID), "ID", "Name");
            return View(mon);
        }

        //
        // GET: /Mon/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Mon mon = db.Mons.Find(id);
            if (mon == null)
            {
                return HttpNotFound();
            }
            return View(mon);
        }

        //
        // POST: /Mon/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Mon mon = db.Mons.Find(id);
            db.Mons.Remove(mon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             Mon mon = db.Mons.Find(id);
			 if (mon == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.Mons.Remove(mon);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}